package com.fastbee.oss.config;

import cn.xuyanwu.spring.file.storage.platform.AliyunOssFileStorage;
import cn.xuyanwu.spring.file.storage.platform.LocalPlusFileStorage;
import cn.xuyanwu.spring.file.storage.platform.QiniuKodoFileStorage;
import cn.xuyanwu.spring.file.storage.platform.TencentCosFileStorage;
import com.fastbee.oss.domain.OssConfig;
import com.fastbee.oss.enums.PlatformType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
@Slf4j
@Component
public class DynamicFileStorage {

    @Resource(name = "PlusFileStorage")
    private List<LocalPlusFileStorage> locallist;

    @Resource(name = "aliyunOssFileStorage")
    private List<AliyunOssFileStorage> aliyunlist;

    @Resource(name = "tencentCosFileStorage")
    private List<TencentCosFileStorage> tencentlist;

    @Resource(name = "qiniuKodoFileStorage")
    private List<QiniuKodoFileStorage> qiniulist;


    public void add(OssConfig config){
        PlatformType systemType = PlatformType.getTypeByValue(config.getPlatformType());
        switch (Objects.requireNonNull(systemType)){
            case LOCAL:
                LocalPlusFileStorage localFileStorage = new LocalPlusFileStorage();
                localFileStorage.setPlatform(config.getPlatform());
                localFileStorage.setBasePath(config.getBasePath());
                localFileStorage.setStoragePath(config.getBasePath());
                localFileStorage.setDomain(config.getDomain());
                locallist.add(localFileStorage);
                break;
            case TENCENT:
                TencentCosFileStorage tencentCosFileStorage = new TencentCosFileStorage();
                tencentCosFileStorage.setPlatform(config.getPlatform());
                tencentCosFileStorage.setBasePath(config.getBasePath());
                tencentCosFileStorage.setSecretId(config.getAccessKey());
                tencentCosFileStorage.setSecretKey(config.getSecretKey());
                tencentCosFileStorage.setDomain(config.getDomain());
                tencentCosFileStorage.setRegion(config.getRegion());
                tencentCosFileStorage.setBucketName(config.getBucketName());
                tencentlist.add(tencentCosFileStorage);
                break;
            case ALIYUN:
                AliyunOssFileStorage aliyunOssFileStorage = new AliyunOssFileStorage();
                aliyunOssFileStorage.setPlatform(config.getPlatform());
                aliyunOssFileStorage.setAccessKey(config.getAccessKey());
                aliyunOssFileStorage.setSecretKey(config.getSecretKey());
                aliyunOssFileStorage.setEndPoint(config.getRegion());
                aliyunOssFileStorage.setBucketName(config.getBucketName());
                aliyunOssFileStorage.setDomain(config.getDomain());
                aliyunOssFileStorage.setBasePath(config.getBasePath());
                aliyunlist.add(aliyunOssFileStorage);
                break;
            case QINIU:
                QiniuKodoFileStorage qiniuKodoFileStorage = new QiniuKodoFileStorage();
                qiniuKodoFileStorage.setPlatform(config.getPlatform());
                qiniuKodoFileStorage.setAccessKey(config.getAccessKey());
                qiniuKodoFileStorage.setSecretKey(config.getSecretKey());
                qiniuKodoFileStorage.setBucketName(config.getBucketName());
                qiniuKodoFileStorage.setDomain(config.getDomain());
                qiniuKodoFileStorage.setBasePath(config.getBasePath());
                qiniulist.add(qiniuKodoFileStorage);
                break;
        }

    }

    public void remove(OssConfig config){
        PlatformType systemType = PlatformType.getTypeByValue(config.getPlatformType());
        switch (Objects.requireNonNull(systemType)){
            case LOCAL:
                locallist.removeIf(localFileStorage -> localFileStorage.getPlatform().equals(config.getPlatform()));
                break;
            case TENCENT:
                tencentlist.removeIf(tencentCosFileStorage -> tencentCosFileStorage.getPlatform().equals(config.getPlatform()));
                break;
            case ALIYUN:
                aliyunlist.removeIf(aliyunOssFileStorage -> aliyunOssFileStorage.getPlatform().equals(config.getPlatform()));
                break;
            case QINIU:
                qiniulist.removeIf(qiniuKodoFileStorage -> qiniuKodoFileStorage.getPlatform().equals(config.getPlatform()));
                break;
        }
    }

    public void update(OssConfig config){
        remove(config);
        add(config);
    }
}
